package com.tiantian.manager;

import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;

import com.music.model.Music;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.tiantian.service.DownloadService;

public class DownloadPlugin extends Plugin {

	@Override
	public PluginResult execute(String action, JSONArray param, String callbackId) {
		
		PluginResult.Status status = PluginResult.Status.OK;
		String result = "";
		try {
			if (action.equals("loadFile")) {
				JSONObject obj= param.getJSONObject(0);
				Music music=new Music();
				music.setName(obj.getString("name"));
				music.setSinger(obj.getString("singer"));
				music.setMusicUrl(obj.getString("musicUrl"));
				//music.setPic(obj.getString("pic"));
				music.setLrc(obj.getString("lrc"));
				System.out.println(obj.toString());
				loadFile(music);
			} else {
				status = PluginResult.Status.INVALID_ACTION;
			}
			return new PluginResult(status, result);
		} catch (Exception e) {
			return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
		}
	}
	public void loadFile(Music music)
	{
		
		Intent intent=new Intent();
		intent.putExtra("music", music);
		intent.setClass(this.ctx, DownloadService.class);
		System.out.println("启动");
		this.ctx.startService(intent);
	}
}
